package com.floreantpos.ui.dialog;

import com.floreantpos.main.Application;
import com.floreantpos.model.Guest;
import com.floreantpos.model.dao.GuestDAO;

public class GuestDialog extends POSDialog {

  /**
   * Creates new form GuestDialog
   */
  public GuestDialog(java.awt.Frame parent, boolean modal, String guestPhone) {
    super(parent, modal);
    initComponents(guestPhone);

    setTitle(Application.getTitle() + ": " + com.floreantpos.POSConstants.CUSTOMER);

    guestView.initialize();
  }

  private void initComponents(String guestPhone) {
    transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
    guestView = new com.floreantpos.ui.views.GuestView(guestPhone);
    transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
    btnFinish = new com.floreantpos.swing.PosButton();
    btnCancel = new com.floreantpos.swing.PosButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    transparentPanel1.setLayout(new java.awt.BorderLayout());

    transparentPanel1.setOpaque(true);
    transparentPanel1.add(guestView, java.awt.BorderLayout.CENTER);

    transparentPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 10, 5));

    btnFinish.setText(com.floreantpos.POSConstants.FINISH);
    btnFinish.setPreferredSize(new java.awt.Dimension(140, 50));
    btnFinish.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doFinishGuest(evt);
      }
    });

    transparentPanel2.add(btnFinish);

    btnCancel.setText(com.floreantpos.POSConstants.CANCEL);
    btnCancel.setPreferredSize(new java.awt.Dimension(140, 50));
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCancelActionPerformed(evt);
      }
    });

    transparentPanel2.add(btnCancel);

    transparentPanel1.add(transparentPanel2, java.awt.BorderLayout.SOUTH);

    getContentPane().add(transparentPanel1, java.awt.BorderLayout.CENTER);

    pack();
  }

  private void doFinishGuest(java.awt.event.ActionEvent evt) {

    String phoneNumber = guestView.getPhoneNumber();
    String name = guestView.getName();
    String addressLine1 = guestView.getAdressLine1();
    String addressLine2 = guestView.getAdressLine2();
    String addressLine3 = guestView.getAdressLine3();
    Double deliveryCharge = guestView.getDeliveryCharge();

    if (name.isEmpty()) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.NAME_REQUIRED);
      return;
    } else if (addressLine1.isEmpty()) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.ADDRESS1_REQUIRED);
      return;
    }

    GuestDAO dao = GuestDAO.getInstance();

    Guest registeredGuest = null;

    registeredGuest = dao.findGuestByTelephone(phoneNumber);

    if (registeredGuest == null) {
      Guest newGuest = new Guest();

      newGuest.setName(name);
      newGuest.setTelephone(phoneNumber);
      newGuest.setAddressLine1(addressLine1);
      newGuest.setAddressLine2(addressLine2);
      newGuest.setAddressLine3(addressLine3);
      newGuest.setDeliveryCharge(deliveryCharge);

      GuestDAO guestDAO = new GuestDAO();
      guestDAO.save(newGuest);

      canceled = false;
      dispose();
    } else {
      POSMessageDialog.showError(com.floreantpos.POSConstants.PHONE_ALREADY_REGISTERED);
    }

  }

  private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
    canceled = true;
    dispose();
  }

  private com.floreantpos.swing.PosButton btnCancel;
  private com.floreantpos.swing.PosButton btnFinish;
  private com.floreantpos.ui.views.GuestView guestView;
  private com.floreantpos.swing.TransparentPanel transparentPanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel2;

}
